﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Paper_Management_System
{
    public partial class MainForm : Form
    {
        public IContainer ctnPaper;
        public MainForm()
        {
            InitializeComponent();
            OnLoad();
            ctnPaper = new MyDBContainer(@"D:\Paper Management System\Paper Management System\data\db_PMS.mdb");
        }
        private void OnLoad()
        {
            //界面设计，加载grdPapers的列信息
            DataGridViewTextBoxColumn colTitle = new DataGridViewTextBoxColumn();
            colTitle.HeaderText = "论文名";
            grdPapers.Columns.Add(colTitle);
            DataGridViewTextBoxColumn colAuthor = new DataGridViewTextBoxColumn();
            colAuthor.HeaderText = "作者";
            grdPapers.Columns.Add(colAuthor);
            DataGridViewTextBoxColumn colKeyWords = new DataGridViewTextBoxColumn();
            colKeyWords.HeaderText = "关键字";
            grdPapers.Columns.Add(colKeyWords);
            DataGridViewTextBoxColumn colAbstract = new DataGridViewTextBoxColumn();
            colAbstract.HeaderText = "摘要";
            grdPapers.Columns.Add(colAbstract);
        }

        public void ReLoadData()
        {
            grdPapers.Rows.Clear();
            for (int i = 0; i < ctnPaper.GetCount(); i++)
            {
                grdPapers.Rows.Add();
                grdPapers[0, i].Value = ctnPaper.Get(i).Title;
                grdPapers[1, i].Value = ctnPaper.Get(i).Author;
                grdPapers[2, i].Value = ctnPaper.Get(i).KeyWords;
                grdPapers[3, i].Value = ctnPaper.Get(i).Abstract;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddForm addForm = new AddForm();
            addForm.paForm = this;
            addForm.Mode = true;
            addForm.ShowDialog();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.grdPapers.CurrentRow.Cells[0].Value==null)
            {
                //选中行的第一列为空时不执行
            }
            else
            {
                AddForm modifyForm = new AddForm();
                modifyForm.paForm = this;
                modifyForm.Mode = false;
                modifyForm.Text = "修改记录";
                //取出被修改的记录，条件：ctnpaper与grdpaper的索引一致
                int idx = this.grdPapers.CurrentRow.Index;
                modifyForm.tbxTitle.Text = this.ctnPaper.Get(idx).Title;
                modifyForm.tbxAuthor.Text = this.ctnPaper.Get(idx).Author;
                modifyForm.tbxKeyWords.Text = this.ctnPaper.Get(idx).KeyWords;
                modifyForm.tbxAbstract.Text = this.ctnPaper.Get(idx).Abstract;
                modifyForm.ShowDialog();
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (this.grdPapers.CurrentRow.Cells[0].Value != null)
            {
                int idx = this.grdPapers.CurrentRow.Index;
                this.ctnPaper.Remove(idx);
                //this.grdPapers.Rows.Remove(this.grdPapers.CurrentRow);
                this.ReLoadData();
            }
            else
            { }            
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryForm queryForm = new QueryForm();
            queryForm.paForm = this;            
            queryForm.ShowDialog();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.ReLoadData();
        }
    }
}
